home *** CD-ROM | disk | FTP | other *** search
- Path: airdmhor.gen.nz!not-for-mail
- From: gumboot@airdmhor.gen.nz (Simon Hosie)
- Newsgroups: comp.lang.c
- Subject: Re: How to get a random strin
- Date: 26 Feb 1996 10:03:37 +1300
- Organization: Airdmhor : a couple of BBS's, a bunch of people, and a cat.
- Message-ID: <4gqir9$d5r@airdmhor.gen.nz>
- References: <4g19id$p7n@gail.ripco.com> <Dn5E0J.GKL@thinkage.on.ca>
- NNTP-Posting-Host: airdmhor.gen.nz
- X-Newsreader: TIN [version 1.2 PL2]
-
- In article <4g19id$p7n@gail.ripco.com> mambuhl@ripco.com (Martin Ambuhl) writes:
- > chancl@nevada.edu (Clapton Chan) in <4fh5od$qq0@news.nevada.edu> asks:
- >
- > [A poor practice follows]
- > if you do not #include <time.h>, you need
- > srand((unsigned)time(NULL));
-
- Alan Bowler:
- > Worse than poor. time() returns a time_t which might not be an
- > integer type. The above code implicitly declares "time()" as returning
- > int. Adding the cast to unsigned will not change that fact that
- > this implicit declaration means your code could be looking in the
- > wrong place. For example: Suppose "time_t" is actually "double"
- > and not "int", and that the machine uses has separate floating
- > point and integer registers. (x86, pdp-11, ibm/370). Then the
- > above code could well result in the same vale being passed to srand()
- > every time the program is called, because time() set the floating
- > point result register and the above code is looking at an integer
- > result register which untouched by time().
-
- I don't follow that.. if time returns a float then won't the result be
- cast and truncated to an int?
-
- Is the following legal, by the way?
-
- char Temp[sizeof(time_t) + sizeof(unsigned)];
-
- time((time_t *)Temp);
- srand(*(unsigned *)Temp);
-